package com.hyg.lib_music.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hyg.lib_common.Base.BaseApplication;
import com.hyg.lib_common.DataModel.Music.MusicCategoryData;
import com.hyg.lib_music.R;
import com.makeramen.roundedimageview.RoundedImageView;

import me.drakeet.multitype.ItemViewBinder;

public class MusicCategoryItemViewBinder  extends ItemViewBinder<MusicCategoryData, MusicCategoryItemViewBinder.ViewHolder> {

    private MusicCategoryViewBinder.OnMusicCategoryClickListener onMusicCategoryClickListener;

    @Override
    protected @NonNull
    ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_music_category, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull MusicCategoryData musicCategoryData) {
        holder.setData(musicCategoryData);
    }


    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public MusicCategoryData musicCategoryData;
        private @NonNull
        LinearLayout music_category;
        private @NonNull
        RoundedImageView ico;
        private @NonNull
        TextView cateName;
        private @NonNull
        TextView cateMessage;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            music_category = itemView.findViewById(R.id.music_category);
            ico = itemView.findViewById(R.id.ico);
            cateName = itemView.findViewById(R.id.cateName);
            music_category.setOnClickListener(this);
        }

        void setData(@NonNull final MusicCategoryData musicCategoryData) {
            this.musicCategoryData = musicCategoryData;
            Glide.with(BaseApplication.getInstance()).load(musicCategoryData.logoUrl).error(R.mipmap.headimg).into(ico);
            cateName.setText(musicCategoryData.cateName);
        }

        @Override
        public void onClick(View view) {
            if (onMusicCategoryClickListener != null) {
                if (view.getId() == R.id.music_category) {
                    onMusicCategoryClickListener.onItemClick(view, this.musicCategoryData);
                }
            }
        }
    }

    // 自定义点击事件
    public void setOnMusicCategoryClickListener(MusicCategoryViewBinder.OnMusicCategoryClickListener onMusicCategoryClickListener) {
        this.onMusicCategoryClickListener = onMusicCategoryClickListener;
    }

    public interface OnMusicCategoryClickListener {
        void onItemClick(View view, MusicCategoryData musicCategoryData);
    }
}